home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / javasprites / src / javaduke.java next >
Encoding:
Java Source  |  2000-09-28  |  3.9 KB  |  119 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. import java.awt.*;
  9. import java.io.*;
  10. /**
  11.  * Based on the JavaDuke Applet by James Gosling.
  12.  */
  13. public class JavaDuke extends Panel implements Runnable {
  14. //_______________________ CLASS METHODS
  15.     public JavaDuke(String dir) throws Exception {
  16.         super();
  17.  
  18.         // used to monitor the image importing process
  19.         MediaTracker tracker = new MediaTracker(this);
  20.         
  21.         // import gif images
  22.         File fDir = new File(dir);
  23.         if (fDir.isDirectory() == false)
  24.             throw new FileNotFoundException (dir);
  25.         if (dir.charAt (dir.length() - 1) != File.separatorChar)
  26.             dir += File.separatorChar;
  27.         for (int i=0; i < nimgs; i++) {        
  28.             images[i] = Toolkit.getDefaultToolkit().getImage(dir + "T" + i + ".gif");
  29.             tracker.addImage(images[i], i);
  30.         }
  31.         
  32.         // wait for them all to finish importing
  33.         try { tracker.waitForAll(); }
  34.         catch (InterruptedException e) { }
  35.         
  36.         // we do nothing about no duke images
  37.         if (tracker.isErrorAny()) throw new Exception ("Duke:Error in Media Tracker");
  38.    }
  39.  
  40. //_______________________ INSTANCE VARIABLES
  41.     private int      loopslot   = -1;   // The current loop slot.
  42.     private Thread   kicker     = null; // The thread animating the images.
  43.     private int      pause      = 100;  // The length of the pause between revs.
  44.     private int      offset     = -57;
  45.     private int      xOffset;
  46.     private int      speed      = 100;
  47.     private int      nimgs      = 17;
  48.     private int      maxWidth   = 130;
  49.     private Image    images[] = new Image[nimgs];
  50.     private Image    offscreenImage;
  51.     private Graphics offscreenGraphics;
  52.  
  53. //_______________________ INSTANCE METHODS
  54.     /** Run the image loop. This method is called by class Thread. */
  55.     public void run() {
  56.         Thread.currentThread().setPriority(Thread.NORM_PRIORITY-1);
  57.  
  58.         // create offscreen image and graphics objects
  59.         offscreenImage    = createImage(images[0].getWidth(this), images[0].getHeight(this));
  60.         offscreenGraphics = offscreenImage.getGraphics();
  61.  
  62.         // take care of positioning the images across the screen
  63.         Dimension d = getSize();
  64.         if (nimgs > 1) {
  65.             if (offset < 0) xOffset = d.width - maxWidth;
  66.             while (kicker != null) {
  67.                 d = getSize();
  68.                 if (++loopslot >= nimgs) {
  69.                     loopslot = 0;
  70.                     xOffset += offset;
  71.                     if (xOffset < 0) xOffset = d.width - maxWidth;
  72.                     else if (xOffset + maxWidth > d.width) xOffset = 0;
  73.                 }
  74.                 repaint();
  75.                 try { Thread.sleep(speed + ((loopslot == nimgs - 1) ? pause : 0)); }
  76.                 catch (InterruptedException e) { break;    }
  77.             }
  78.         }
  79.     }
  80.  
  81.     /** Should not get called because we are using MediaTracker.waitForAll(). */
  82.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {
  83.         if ((flags & (SOMEBITS|FRAMEBITS|ALLBITS)) != 0) {
  84.             if ((images != null) && (loopslot < nimgs) && (images[loopslot] == img)) {
  85.                 repaint();
  86.             }
  87.         }
  88.         return (flags & (ALLBITS|ERROR)) == 0;
  89.     }
  90.  
  91.     /** Paint the current frame using our offscreen graphics to reduce flicker. */
  92.     public void paint(Graphics g) {
  93.         if (loopslot < 0) return;
  94.         if ((images != null) && (loopslot < nimgs) && (images[loopslot] != null)) {
  95.             offscreenGraphics.drawImage(images[loopslot], 0, 0, this);
  96.             g.drawImage(offscreenImage, xOffset, 0, this);
  97.         }
  98.     }
  99.  
  100.     /** Start the animation by forking an animation thread. */
  101.     public void start() {
  102.         if (kicker == null) {
  103.             kicker = new Thread(this);
  104.             kicker.start();
  105.         }
  106.     }
  107.  
  108.     /** Stop the animation. The thread will exit because kicker is set to null. */
  109.     public void stop() {
  110.         if (kicker != null) {
  111.             kicker.stop();
  112.             kicker = null;
  113.         }
  114.     }
  115.  
  116.     /** So the layout manager respects our preferred size. */
  117.     public Dimension getPreferredSize() { return new Dimension(5, 80); }
  118. }
  119.